/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iti.jets.gpts.bean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.primefaces.event.map.OverlaySelectEvent;
import org.primefaces.model.map.Circle;

import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.LatLng;

import org.primefaces.model.map.MapModel;
import org.primefaces.model.map.Marker;

@ManagedBean
@ViewScoped
public class PointBean implements Serializable {

    /**
     * Creates a new instance of PointBean
     */
    private int counter = 0;
    private String title;
    private double lat;
    private double lng;
    private String type;
    private MapModel simpleModel;
    private String request = "";
    private List<Marker> terminalMarkers = new ArrayList<Marker>();
    private List<Marker> pointsMarkers = new ArrayList<Marker>();

    public PointBean() {
        simpleModel = new DefaultMapModel();
        LatLng coord1 = new LatLng(36.879466, 30.667648);
        LatLng coord2 = new LatLng(36.883707, 30.689216);
        LatLng coord3 = new LatLng(36.879703, 30.706707);
        LatLng coord4 = new LatLng(36.885233, 30.702323);
        simpleModel.addOverlay(new Marker(coord1, "Konyaalti", "10", "http://maps.google.com/mapfiles/ms/micons/blue-dot.png"));
        simpleModel.addOverlay(new Marker(coord2, "Ataturk Parki", "1"));
        simpleModel.addOverlay(new Marker(coord4, "Kaleici", "21", "http://maps.google.com/mapfiles/ms/micons/pink-dot.png"));
        simpleModel.addOverlay(new Marker(coord3, "Karaalioglu Parki", "2", "http://maps.google.com/mapfiles/ms/micons/yellow-dot.png"));
    }

    public void onMarkerSelect(OverlaySelectEvent event) {
        if (event.getOverlay() instanceof org.primefaces.model.map.Marker) {
            Marker marker = (Marker) event.getOverlay();
            System.out.println("asdasdas==========");
            if (counter == 0) {

                System.out.println("t1marker" + marker.getTitle() + "=======" + marker.getData().toString() + "===========" + marker.getLatlng());
                terminalMarkers.add(marker);

                counter++;

            } else if (counter == 1) {

                if (!marker.equals(terminalMarkers.get(0))) {

                    terminalMarkers.add(marker);
                    System.out.println("t2marker" + marker.getTitle() + "=======" + marker.getData().toString() + "===========" + marker.getLatlng());
                    counter++;

                }
            } else {
                pointsMarkers.add(marker);

                System.out.println("pmarker" + marker.getTitle() + "=======" + marker.getData().toString() + "===========" + marker.getLatlng());
                Circle circle2 = new Circle(new LatLng(marker.getLatlng().getLat(), marker.getLatlng().getLng()), 100);
                circle2.setStrokeColor("#00ff00");
                circle2.setFillColor("#00ff00");
                circle2.setStrokeOpacity(0.7);
                circle2.setFillOpacity(0.7);
                simpleModel.addOverlay(circle2);
            }
        }
    }

    public void save() {
        double t1Lat = terminalMarkers.get(0).getLatlng().getLat();
        double t1Lng = terminalMarkers.get(0).getLatlng().getLng();
        double t2Lat = terminalMarkers.get(1).getLatlng().getLat();
        double t2Lng = terminalMarkers.get(1).getLatlng().getLng();

        if (terminalMarkers.size() == 2) {
            request += "{";
            request += "origin: (" + t1Lat + "," + t1Lng + "),";
            request += "destination: (" + t2Lat + "," + t2Lng + "),";
            request += "travelMode: google.maps.TravelMode.DRIVING";
            request += "}";
            System.out.println(request);
        } else {
            System.out.println("null");
        }
    }

    public void newPoint() {
        System.out.println("newPoint======" + lat + "," + lng + " title: " + title);
        if (type.equals("Terminal")) {
            Marker newpoint = new Marker(new LatLng(lat, lng), title, "id", "http://maps.google.com/mapfiles/ms/micons/pink-dot.png");
            simpleModel.addOverlay(newpoint);
        } else if (type.equals("Stop")) {
            Marker newpoint = new Marker(new LatLng(lat, lng), title, "id", "http://maps.google.com/mapfiles/ms/micons/yellow-dot.png");
            simpleModel.addOverlay(newpoint);
        } else {
            Marker newpoint = new Marker(new LatLng(lat, lng), title, "id", "http://maps.google.com/mapfiles/ms/micons/blue-dot.png");
            simpleModel.addOverlay(newpoint);
        }
    }

    public List<Marker> getTerminalMarkers() {
        return terminalMarkers;
    }

    public void setTerminalMarkers(List<Marker> terminalMarkers) {
        this.terminalMarkers = terminalMarkers;
    }

    public List<Marker> getPointsMarkers() {
        return pointsMarkers;
    }

    public void setPointsMarkers(List<Marker> pointsMarkers) {
        this.pointsMarkers = pointsMarkers;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public double getLat() {
        return lat;
    }

    public void setLat(double lat) {
        this.lat = lat;
    }

    public double getLng() {
        return lng;
    }

    public void setLng(double lng) {
        this.lng = lng;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public MapModel getSimpleModel() {
        return simpleModel;
    }

    public void setSimpleModel(MapModel simpleModel) {
        this.simpleModel = simpleModel;
    }

    public String getRequest() {
        return request;
    }

    public void setRequest(String request) {
        this.request = request;
    }

}
